package net.sinodata.smartframework.util;

import net.sinodata.smartframework.component.SpringContext;
import net.sinodata.smartframework.constant.CommonConsts;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.InputStream;
import java.util.Properties;

/**
 * Description: PropertiesUtils设计为单例模式 专门用于读取param-config-*.properties
 * 
 * @author: yinliangyun
 * @date: 2021-5-18
 */
public class PropertiesUtils {

	private static final Logger logger = LoggerFactory.getLogger(PropertiesUtils.class);

	private static PropertiesUtils singleton = new PropertiesUtils();

	private static Properties properties;

	/**
	 * Description: 构造函数私有化
	 */
	private PropertiesUtils() {
		loadProperties();
	}

	/**
	 * Description: 提供公共的方法获取PropertiesUtils对象
	 * 
	 * @return
	 */
	public static PropertiesUtils getInstance() {
		logger.info("PropertiesUtils getInstance success");
		return singleton;
	}

	/**
	 * Description: 根据key值获取param-config-*.properties文件中的value值
	 * 
	 * @param key
	 * @return
	 */
	public String getProperty(String key) {
		return properties.getProperty(key);
	}

	/**
	 * Description: 根据key值获取param-config-*.properties文件中的value值,如果value值为null则返回默认值defaultValue
	 * 
	 * @param key
	 * @param defaultValue
	 * @return
	 */
	public String getProperty(String key, String defaultValue) {
		return properties.getProperty(key, defaultValue);
	}

	/**
	 * Description: 类加载时执行此方法加载param-config-*.properties文件中的值到Properties properties静态对象中
	 */
	private void loadProperties() {
		logger.info("PropertiesUtils loadProperties start");
		properties = new Properties();
		String propertiesName = getPropertiesName();
		logger.info("PropertiesUtils loadProperties propertiesName : {}", propertiesName);
		try (InputStream inputStream = PropertiesUtils.class.getClassLoader().getResourceAsStream(propertiesName)) {
			// 第一种方式：通过类加载器进行获取properties文件流 --> inputStream = PropertiesUtils.class.getClassLoader().getResourceAsStream("config.properties");
			// 第二种方式：通过类进行获取properties文件流 --> inputStream = PropertiesUtils.class.getResourceAsStream("/" + configPath);
			// 第三种方式：通过FileInputStream进行获取properties文件流 --> inputStream = new FileInputStream(configPath);
			properties.load(inputStream);
		} catch (Exception e) {
			logger.error("PropertiesUtils loadProperties Exception" + "_" + e.getMessage(), e);
		}
		logger.info("PropertiesUtils loadProperties end properties : " + properties);
	}
	
	/**
	 * Description: 获取系统当前环境使用的配置文件名称
	 * 
	 * @return
	 */
	private String getPropertiesName() {
		String propertiesName = "";
		String springActiveProfile = SpringContext.getActiveProfile();
		logger.info("PropertiesUtils getPropertiesName springActiveProfile : {}", springActiveProfile);
		if (CommonConsts.SPRING_ACTIVE_PROFILE_DEV.equals(springActiveProfile)) {
			propertiesName = CommonConsts.PARAM_DEV_PROPERTIES;
		} else if (CommonConsts.SPRING_ACTIVE_PROFILE_SIT.equals(springActiveProfile)) {
			propertiesName = CommonConsts.PARAM_SIT_PROPERTIES;
		} else if (CommonConsts.SPRING_ACTIVE_PROFILE_UAT.equals(springActiveProfile)) {
			propertiesName = CommonConsts.PARAM_UAT_PROPERTIES;
		} else if (CommonConsts.SPRING_ACTIVE_PROFILE_PP.equals(springActiveProfile)) {
			propertiesName = CommonConsts.PARAM_PP_PROPERTIES;
		} else if (CommonConsts.SPRING_ACTIVE_PROFILE_PROD.equals(springActiveProfile)) {
			propertiesName = CommonConsts.PARAM_PROD_PROPERTIES;
		}
		return propertiesName;
	}
	
}
